package gui.commands.stateDiagramCommands;

import data.DataVector;
import data.classDiagram.ClassEntity;
import data.classDiagram.Entity;
import data.classDiagram.Trigger;
import data.stateDiagram.ConditionalTransition;
import data.stateDiagram.State;
import gui.commands.ICommand;

/**
 * User: Szymon Kudzia
 * Date: 12.05.13
 * Time: 14:39
 */
public class AddConditionalTransitionCommand implements ICommand {
	protected Trigger trig;
	protected ConditionalTransition t;
	protected ClassEntity entity;

	protected String condition;
	protected String trueExpression;
	protected String falseExpression;
	protected String action;
	protected String failureAction;
	protected String message;
	protected String failureMessage;
	protected String trigger;
	protected String source;
	protected String destination;
	protected String failureDestination;

	public AddConditionalTransitionCommand(String condition, String trueExpression, String falseExpression,
	                                       String action, String failureAction, String message, String failureMessage,
	                                       String trigger, String source, String destination, String failureDestination) {
		this.condition = condition;
		this.trueExpression = trueExpression;
		this.falseExpression = falseExpression;
		this.action = action;
		this.failureAction = failureAction;
		this.message = message;
		this.failureMessage = failureMessage;
		this.trigger = trigger;
		this.source = source;
		this.destination = destination;
		this.failureDestination = failureDestination;
	}

	@Override
	public void execute() {
		trig = new Trigger();
		trig.setName(trigger);
		trig.setVisibility("+");

		t = new ConditionalTransition();
		t.setCondition(condition);
		t.setTrueExpression(trueExpression);
		t.setFalseExpression(falseExpression);
		t.setAction(action);
		t.setFailureAction(failureAction);
		t.setMsgs(message);
		t.setFailureMsg(failureMessage);
		t.setTrigger(trig);

		State src = null;
		State dst = null;
		State fdst = null;

		for (State s : DataVector.getInstance().getActiveStateDiagram().
				getStateDiagram().getStates()){

			if (s.getName().equals(source))
				src = s;
			if (s.getName().equals(destination))
				dst = s;

			if (s.getName().equals(failureDestination))
				fdst = s;
		}

		t.setDestination(dst);
		t.setSource(src);
		t.setFailureDestination(fdst);

		entity = DataVector.getInstance().getActiveStateDiagram();
		entity.addTrigger(trig);
		entity.getStateDiagram().addTransition(t);

		DataVector.getInstance().refresh();
	}

	@Override
	public void undo() {
		entity.getTriggers().remove(trig);
		entity.getStateDiagram().getTransitions().remove(t);

		DataVector.getInstance().refresh();
	}

	@Override
	public void redo() {
		entity.addTrigger(trig);
		entity.getStateDiagram().addTransition(t);

		DataVector.getInstance().refresh();
	}
}
